<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  if($_POST['status'] == 1)
  {
		if($_POST['processNo'] == 3)
		{
			$setVariable = " status = 'outgoing', okNotOk1 = 1, okNotOk2 = 1, okNotOk3 = 1 ";
			$serLogValue = " status = 'outgoing' ";
		}
		else if($_POST['processNo'] == 2)
		{
			$setVariable = " status = 'inlogsheet', okNotOk1 = 1, okNotOk2 = 1";
			$serLogValue = " status = '1' ";
		}
		else if($_POST['processNo'] == 1)
		{
			$setVariable = " status = 'inlogsheet', okNotOk1 = 1";
			$serLogValue = " status = '1' ";
		}
		echo $updatePlanRecord = "UPDATE planning 
		                        SET ".$setVariable."
		                      WHERE planningId = ".$_POST['planningId'];
		$updatePlanRecordRes = mysql_query($updatePlanRecord);

    $checkProcess = "SELECT noOfProcess, okNotOk1, okNotOk2, okNotOk3 FROM planning WHERE planningId = ".$_POST['planningId'];
    $checkProcessRes = mysql_query($checkProcess);
    if($row = mysql_fetch_array($checkProcessRes))
    {
    	$noOfOks = 0;
    	if($row['okNotOk1'] > 0)
    	{
    		$noOfOks = $noOfOks + 1;
    	}
    	if($row['okNotOk2'] > 0)
    	{
    		$noOfOks = $noOfOks + 1;
    	}
    	if($row['okNotOk3'] > 0)
    	{
    		$noOfOks = $noOfOks + 1;
    	}        
    	
    	if($row['noOfProcess'] == $noOfOks)
    	{
    		$updatePlanning = "UPDATE planning SET status = 'outgoing' WHERE planningId = ".$_POST['planningId'];
    		$updateRes = mysql_query($updatePlanning);
    		
    		$updateIncomming = "UPDATE incoming SET status = 'outgoing' WHERE incomingId =".$_POST['planningId'];
        $updateIncommingRes = mysql_query($updateIncomming);
        
        $serLogValue = " status = 'outgoing' ";
    	}
	  }
    $updateLogStatus = "UPDATE logsheet
                          SET ".$serLogValue."
                        WHERE planningId =".$_POST['planningId']."
                          AND logSheetId <= ".$_POST['logSheetId'];
    $updateLogStatusRes = mysql_query($updateLogStatus);
    if($updateLogStatusRes)
    {
    	return true;
    }
    else
    {
    	return true;
    }
	}
	else if($_POST['status'] == -1)
	{
		$smarty->assign("planningId",$_POST['planningId']);
		$smarty->assign("logSheetId",$_POST['logSheetId']);
		$smarty->assign("processNo",$_POST['processNo']);
		$smarty->assign("status",$_POST['status']);
	  $smarty->display("statusChange.tpl");
	}
//	else if($_POST['status'] == -1)
//	{
//	  $status = '-1';
//	  //Effecting of Planning Record : Starts
//	  $selPlanning = "SELECT incomingId, partyId, date, quantity, weight, processId1, processId2, processId3, noOfProcess, status, parentPlanningId
//	                    FROM planning
//	                   WHERE planningId =".$_POST['planningId'];
//    $selPlanningRes = mysql_query($selPlanning);
//    if($planRow = mysql_fetch_array($selPlanningRes))
//    {
//    	if($planRow['date'] == NULL)
//    	{
//    		$planRow['date'] = date('Y-m-d');
//    	}
//    	$insNewRecord = "INSERT INTO planning (incomingId, partyId, date, quantity, weight, processId1, processId2, processId3, noOfProcess,status, parentPlanningId)
//    	                  VALUE (".$planRow['incomingId'].",".$planRow['partyId'].",'".$planRow['date']."',".$planRow['quantity'].",".$planRow['weight'].",
//    	                         ".$planRow['processId1'].",".$planRow['processId2'].",".$planRow['processId3'].",".$planRow['noOfProcess'].",'inplanning',
//    	                         ".$_POST['planningId'].")";
//      $insNewRecordRes = mysql_query($insNewRecord);
//    }
//    
//    if($insNewRecordRes)
//    {
//    	$updatePlanRecord = "UPDATE planning 
//    	                        SET status = 'not ok'
//    	                      WHERE planningId = ".$_POST['planningId'];
//      $updatePlanRecordRes = mysql_query($updatePlanRecord);
//    }
//	  //Effecting of Planning Record : Ends
//	  $updateLogStatus = "UPDATE logsheet
//                           SET status = '".$status."'
//                         WHERE planningId =".$_POST['planningId'];
//    $updateLogStatusRes = mysql_query($updateLogStatus);
//    if($updateLogStatusRes)
//    {
//    	return true;
//    }
//    else
//    {
//    	return true;
//    }
//	}
}
?>